package com.firmiana.jt809.config;

import com.firmiana.jt809.entity.ParameterModel;
import com.firmiana.jt809.protocol.IReceiveProtocol;
import com.firmiana.jt809.protocol.ISendProtocol;
import com.firmiana.jt809.protocol.receive.MainConnectProtocol;
import com.firmiana.jt809.protocol.receive.MainLinkCarInfoExgMsgProtocol;
import com.firmiana.jt809.protocol.receive.SubLinkConnectResponseProtocol;
import com.firmiana.jt809.protocol.send.MainLinkSendHeartBeat;
import com.firmiana.jt809.protocol.send.SubLinkSendHeartBeat;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author hucl
 * @date 2020/8/25 11:08
 */
public class GlobalConfig {

    /**
     * 流水号（线程安全）
     */
    private static final AtomicLong upCounter = new AtomicLong(0L);

    public static ParameterModel parModel = new ParameterModel();

    private static final Map<Integer, Class<? extends IReceiveProtocol>> receiveProtocolMap = new HashMap<>();
    private static final Map<Integer, Class<? extends ISendProtocol>> sendProtocolMap = new HashMap<>();


    static {
        // 主链路登录
        receiveProtocolMap.put(0x1001, MainConnectProtocol.class);
        // 从链路连接应答
        receiveProtocolMap.put(0x9002, SubLinkConnectResponseProtocol.class);
        // 主链路车辆动态信息交换
        receiveProtocolMap.put(0x1002, MainLinkCarInfoExgMsgProtocol.class);

        // 主链路心跳
        sendProtocolMap.put(0x1006, MainLinkSendHeartBeat.class);
        // 子链路心跳
        sendProtocolMap.put(0x9005, SubLinkSendHeartBeat.class);
    }


    // 获取流水号，用于包的发送
    public static long getSN() {
        return upCounter.getAndIncrement();
    }

    public static Class<? extends IReceiveProtocol> getReceiveProtocolByProtocolId(Integer protocolId) {
        return receiveProtocolMap.get(protocolId);
    }

    public static Class<? extends ISendProtocol> getSendProtocolByProtocolId(Integer protocolId) {
        return sendProtocolMap.get(protocolId);
    }

}
